/*
 *   Copyright (C) 2009  Jens Kieselbach
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You may obtain a copy of the License at http://www.gnu.org/licenses/gpl.html
 */
package de.kieselbach.swing.validation

import java.awt.*
import javax.swing.text.*

public class Validator {

    final static def EMPTY_TEXT_CHECKER = {comp ->
        return comp.text.trim().length() > 0
    }

    // -------------------------------------------------------------------

    static def invalidColor = new Color(255, 180, 180)

    // -------------------------------------------------------------------

    def components = [:] as Map
    def invalidComponents = [] as Set
    def componentsToEnable = [] as Set

    def validate(def component, def checker) {

        if(checker == null) {
            return component
        }

        def validationcomp = components[component]

        if(validationcomp != null) {
            validationcomp.addChecker(checker)
        }
        else {
            if(component instanceof JTextComponent) {
                validationcomp = new ValidationTextComponent(this, component, checker)
                components[component] = validationcomp
            }
            else {
                throw new IllegalArgumentException("no default compoment marker for class: " + component.getClass().getName())
            }
        }

        if(!validationcomp.valid) {
            invalidComponents << validationcomp

            if(invalidComponents.size() == 1) {
                componentsToEnable.each() {comp ->
                    comp.enabled = false
                }
            }
        }

        component
    }

    def enable(def component) {

        if(component != null) {
            componentsToEnable << component
            component.enabled = invalidComponents.size() == 0
        }

        component
    }

    // -------------------------------------------------------------------

    void componentChanged(def validcomp) {

        if(validcomp.valid) {
            invalidComponents.remove(validcomp)

            if(invalidComponents.size() == 0) {
                componentsToEnable.each() {comp ->
                    comp.enabled = true
                    comp.repaint()
                }
            }
        }
        else {
            invalidComponents << validcomp

            if(invalidComponents.size() == 1) {
                componentsToEnable.each() {comp ->
                    comp.enabled = false
                    comp.repaint()
                }
            }
        }
    }
}